﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using H2SDB;
using h2sbk_sharp.DoNetExt;

namespace h2sbk_sharp.Controllers
{
    public class TerminalController : ApiController
    {
        class allresult
        {
            public int terminalid { set; get; }
            public int pointnum { set; get; }
            public string person { set; get; }
            public DateTime? lastupdatetime { set; get; }
            public int needupdate { set; get; }
            public string remark { set; get; }
        }
        [HttpGet, HttpPost]
        public HttpResponseMessage all ()
        {
            using (h2sDataContext db = new h2sDataContext())
            {
                string sqlCmd = @"
                    select
	                    a.id as 'terminalid',
	                    (select COUNT(1) from point b where b.terminalid = a.id) as 'pointnum',
	                    c.realname as 'person',
	                    a.lastImportTime as 'lastupdatetime',
	                    DATEDIFF(day, a.lastImportTime, GETDATE()) - 6 as 'needupdate',
	                    '' as 'remark'
                    from
	                    terminal a
                    left join
	                    [user] c
                    on
	                    a.userid = c.id
                ";
                var result = db.ExecuteQuery<allresult>(sqlCmd).ToArray();
                return result.Select(p => new
                {
                    terminalid = p.terminalid,
                    pointnum = p.pointnum,
                    person = p.person,
                    lastupdatetime = p.lastupdatetime?.ToString("yyyy-MM-dd HH:mm:ss"),
                    needupdate = p.needupdate > 0 ? "是" : "否"
                }).ToHttpResponseMessage();
            }
        }
        [HttpGet, HttpPost]
        public HttpResponseMessage setuser (int id, int userid)
        {
            string rtv = null;
            try
            {
                using (h2sDataContext db = new h2sDataContext())
                {
                    var terminal = db.terminal.Where(p => p.id == id).FirstOrDefault();
                    if (terminal != null)
                    {
                        terminal.userid = userid;
                        db.SubmitChanges();
                        rtv = "ok";
                    }
                    else
                    {
                        rtv = "该终端不存在";
                    }
                }
            }
            catch (Exception e)
            {
                rtv = e.Message;
            }
            return rtv.ToHttpResponseMessage();
        }
        [HttpGet, HttpPost]
        public HttpResponseMessage newcount()
        {
            int count;
            using (h2sDataContext db = new h2sDataContext())
            {
                count = db.terminal.Where(p => p.userid == null).Count();
            }
            return count.ToHttpResponseMessage();
        }

        [HttpGet, HttpPost]
        public HttpResponseMessage allid ()
        {
            using (h2sDataContext db = new h2sDataContext())
            {
                return db.allTerminal().ToHttpResponseMessage();
            }
        }
    }
}
